/*******************************************************************************
 * Copyright (c) 2008, 2009 Fabio Mauro - Alberto Terraneo.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.2
 * or any later version published by the Free Software Foundation.
 *     
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU Free Documentation License for more details.
 *     
 * You should have received a copy of the GNU Free Documentation 
 * License along with this distribution; if not, send a email to authors:
 * aterraneo <at> users.sourceforge.net
 * bigmoby09 <at> users.sourceforge.net
 *******************************************************************************/
package it.blueocean.acanto.model;

import it.blueocean.acanto.model.common.ABaseEntity;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.ForeignKey;

@Entity
@Table(name = "TA_LISTINI")
public class Listino extends ABaseEntity
{
    private static final long serialVersionUID = -5895874004166544885L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID_LISTINO", unique = true, nullable = false)
    private long id;
    
    @Column(name = "DATA_CARICAMENTO", nullable = false, length = 19)
    @Temporal(TemporalType.DATE)
    private Date dataCaricamento;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ID_FORNITORE")
    @ForeignKey(name = "FK_LISTINO_FORNITORE")
    private Fornitore fornitore;
    
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "listino")
    private Set<Prodotto> prodotti = new HashSet<Prodotto>(0);
    
    public Listino(){}
    
    @Override
    public boolean equals(Object obj)
    {
        if (obj instanceof Listino)
        {
            Listino listino = (Listino) obj;
            return 
                listino.getId()== this.getId() && 
                listino.getDataCaricamento().equals(this.getDataCaricamento());
        }
        else
            return false;
    }
    
    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = (int) id;
        result = prime * result + ((dataCaricamento == null) ? 0 : dataCaricamento.hashCode());
        return result;
    }
    
    @Override
    public String toString()
    {
        final String TAB = "    ";

        StringBuilder retValue = new StringBuilder();

        retValue.append("Listino ( ")
        .append(super.toString()).append(TAB)
        .append("id = ").append(this.getId()).append(TAB)
        .append("DataCaricamento = ").append(this.getDataCaricamento()).append(TAB)
        .append(" )");

        return retValue.toString();
    }

    public long getId()
    {
        return id;
    }

    public void setId(long id)
    {
        this.id = id;
    }

    public Fornitore getFornitore()
    {
        return fornitore;
    }

    public void setFornitore(Fornitore fornitore)
    {
        this.fornitore = fornitore;
    }

    public Date getDataCaricamento()
    {
        return dataCaricamento;
    }

    public void setDataCaricamento(Date dataCaricamento)
    {
        this.dataCaricamento = dataCaricamento;
    }

    public Set<Prodotto> getProdotti()
    {
        return prodotti;
    }

    public void setProdotti(Set<Prodotto> prodotti)
    {
        this.prodotti = prodotti;
    }

}
